using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

/// <summary>
/// 资源加载模块
/// </summary>
public class 资源管理器 : 单例_基础管理器<资源管理器>
{
    //同步加载资源
    /// <summary>
    /// 加载资源 如果时GameObject 则自动实例化
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="name"></param>
    /// <returns></returns>
    public T 加载<T>(string name) where T : Object
    {
        T res = Resources.Load<T>(name);
        if (res is GameObject)
            return GameObject.Instantiate(res);
        else
            return res;
    }
    //异步加载资源
    public void 加载_异步<T>(string name, UnityAction<T> 委托) where T : Object
    {
        帧更新控制器.获取实例.开启携程(异步加载(name,委托));
    }


    private IEnumerator 异步加载<T>(string name, UnityAction<T> 委托) where T : Object
    {
        ResourceRequest r = Resources.LoadAsync<T>(name);
        yield return r; 

        if (r.asset is GameObject)
            委托(GameObject.Instantiate(r.asset) as T);
        else
            委托(r.asset as T);
    }
}
